package daniel.erp.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

import daniel.erp.biz.IDepBiz;
import daniel.erp.entity.Dep;

@SuppressWarnings("serial")
public class DepAction extends ActionSupport {
	private IDepBiz depBiz;

	public void setDepBiz(IDepBiz depBiz) {
		this.depBiz = depBiz;
	}
	public Dep dep1;
	public Dep getDep1() {
		return dep1;
	}
	public void setDep1(Dep dep1) {
		this.dep1 = dep1;
	}
	
	public int page;
	public int rows;
	
	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	
	//写json字符串
	public void writeJsonString(String str){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		try {
			response.getWriter().write(str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//显示的部门数据查询
	public void list(){
		//List<Dep> list = depBiz.getAllDep();
		//List<Dep> list2= depBiz.getDepByQuery(dep1,page,rows);
		Map<String, Object> list2= depBiz.getDepByQuery(dep1,page,rows);
		//System.out.println("66"+dep1);
		String str = JSON.toJSONString(list2);
		writeJsonString(str);
	}
	
	//添加部门参数
	public Dep dep;
	
	public Dep getDep() {
		return dep;
	}
	public void setDep(Dep dep) {
		this.dep = dep;
	}
	
	public void add(){
		try{
			depBiz.saveDep(dep);
			writeJsonString(ajaxReturn(true, "新增成功"));
		}catch(Exception c){
			writeJsonString(ajaxReturn(false, "新增失败"));
		}
	}
	
	public String ajaxReturn(Boolean b,String msg){
		Map<String, Object> m=new HashMap<String, Object>();
		m.put("success", b);
		m.put("message", msg);
		return JSON.toJSONString(m);
	}
	
	public Long id;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public void getDepById(){
		Dep depById = depBiz.getDepById(id);
		String jsonStr = JSON.toJSONString(depById);
		String s = json2json(jsonStr, "dep.");
		writeJsonString(s);
		
	}
	
	//给json字符串key加前缀
	public String json2json(String jsonStr,String perfix){
		Map<String, Object> parseObject = JSON.parseObject(jsonStr);
		Map<String, Object> newmap=new HashMap<String, Object>();
		for(String key:parseObject.keySet()){
			newmap.put(perfix+key, parseObject.get(key));
		}
		return JSON.toJSONString(newmap);
	}
	
	public void update(){
		try{
			depBiz.updateDep(dep);
			writeJsonString(ajaxReturn(true, "修改成功"));
		}catch(Exception e){
			writeJsonString(ajaxReturn(false, "修改失败"));
		}
		
	}
	
	public void delete(){
		try{
			depBiz.deleteDep(id);
			writeJsonString(ajaxReturn(true, "删除成功"));
		}catch(Exception e){
			writeJsonString(ajaxReturn(false, "删除失败"));
		}
		
	}
}
